/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-12-05 13:16:54
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-23 12:09:00
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { finalize } from 'rxjs/operators';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/api';
import { SuiteInfoBoardServiceProxy, MatingManagerServiceProxy, ProductionDesignServiceProxy ,PartsOutboundInput} from '@shared/service-proxies/service-proxies';
import { TreeNode } from 'primeng/api';


@Component({
    selector: 'app-matching-sum',
    templateUrl: './matching-sum.component.html',
    styleUrls: ['./matching-sum.component.less']
})
export class MatchingSumComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Input() editData: any;  //行数据
    saving = false;
    files2 = [];
    tableData = [];
    selectedFiles: any = {};
    loadingtree = false;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _suiteInfoBoardServiceProxy: SuiteInfoBoardServiceProxy,
        private _matingManagerServiceProxy: MatingManagerServiceProxy,
        private _productionDesignServiceProxy: ProductionDesignServiceProxy,
    ) {
        super(injector);
    }
    getTreeList() {
        this.loadingtree = true;
        this._suiteInfoBoardServiceProxy.getProductionDesigenTree(
            this.editData.id,
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                let arr = []
                arr.push(result)
                this.files2 = [];
                this.files2 = this.isEqual(JSON.parse(JSON.stringify(arr)))
                if (Object.keys(result).length != 0) {
                    this.expandAll();
                }
                this.loadingtree = false;
            });
    }
    // 加载工序工步节点 
    getProductionTechquine(nodeId, node) {
        this._productionDesignServiceProxy.getProductionTechquine(
            nodeId
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                let childNodes = this.isEqual(JSON.parse(JSON.stringify(result)))
                node.children = childNodes
                node.treeList = childNodes
            });
    }
    // 组合树结构数据
    isEqual(obj1) {
        for (const iterator of obj1) {
            iterator.label = iterator.nodeName
            if (iterator.treeList) {
                iterator.children = iterator.treeList
                this.isEqual(iterator.treeList)
            }
        }
        return obj1
    }
    private expandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.expandRecursive(childNode, isExpand);
            });
        }
    }
    expandAll() {
        this.files2[0].children = this.files2[0].treeList;
        this.files2[0].expanded = true;
        this.files2[0].children.forEach((node) => {
            // this.children(node);
            this.expandRecursive(node, true);
        });
    }
    nodeSelect(event) {
        this.selectedFiles = event.node;
        let inp = new PartsOutboundInput()
        inp.nodeId = this.selectedFiles.id
        abp.ui.setBusy();
        this._matingManagerServiceProxy.displayMatingNodeDetails(
            inp,
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                abp.ui.clearBusy();
                if (result.code != 0) {
                    this._messageService.error(result.message);
                }
                this.tableData = result.matingTreeGridList;
                this.expandAllTree(this.tableData);
            });
        if (event.node.isRelation === 1) {//树结构判断是否有子节点
            event.node.children = []
            event.node.treeList = []
            this.getProductionTechquine(event.node.id, event.node)
        }

    }
     // 展开
     expandAllTree(rs) {
        for (const node of rs) {
            this.TreeExpandRecursive(node, true);
        }
    }
    private TreeExpandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.TreeExpandRecursive(childNode, isExpand);
            });
        }
    }
    ngOnInit() {
    }
    onShown(): void {
        this.getTreeList();
    }
    show(): void {
        this.modal.show();
    }
    //关闭事件
    close(): void {
        this.tableData = [];
        this.modal.hide();
    }
}
